package com.tourlaid.mamagerment.ao.pc.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.pc.ShopAo
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.Shop
import com.tourlaid.mamagerment.service.ShopService
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.vo.ShopListVo
import com.tourlaid.mamagerment.vo.ShopVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/21.
 */
@Service("shopAo")
class ShopAoImpl : ShopAo {

    @Resource
    val shopService: ShopService? = null

    override fun list(pageNo: Int, pageSize: Int): ResponseData<ArrayList<ShopListVo>> {
        var voList = ArrayList<ShopListVo>()
        var responseData = ResponseData(voList)
        PageHelper.startPage<Shop>(pageNo, pageSize, true)
        var page = shopService!!.queryForList()!! as Page<Shop>
        responseData.totalCount = page.getTotal()
        val shopList = page.getResult()
        if (!CollectionUtils.isEmpty(shopList)) {
            for (shop in shopList) {
                var vo = ShopListVo()
                BeanUtils.copyProperties(shop, vo)
                vo.createTime = DateUtil.getFormantDate(shop.createTime, DateUtil.DATA_FORMANT3)
                voList.add(vo)
            }
        }
        return responseData
    }

    override fun insert(shopName: String?) {
        shopService!!.insert(shopName)
    }

    override fun detail(shopId: Long?): ShopVo {
        var vo = ShopVo()
        var shop = shopService!!.getById(shopId)
        shop ?: return vo
        vo.id = shop.id
        vo.shopName = shop.shopName
        return vo
    }

    override fun update(shopId: Long?, shopName: String?) {
        shopService!!.update(shopId, shopName)
    }

    override fun delete(shopId: Long?) {
        shopService!!.delete(shopId)
    }

}